//
//  UpCircleViewDownLable.m
//
//
//  Created by gynjobs on 2017/8/11.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "UpCircleViewDownLable.h"

@interface UpCircleViewDownLable ()
@property(strong,nonatomic)UIButton *UpButton;
@property(strong,nonatomic)UILabel *downLable;
@property(strong,nonatomic)UIView *outskirtsAlphView;
@end

@implementation UpCircleViewDownLable
-(instancetype)init{
    self = [super init];
    if (self) {
        
        [self addSubWidghtForView];
    }
    return self;
}
-(void)addSubWidghtForView{
    //添加底部的功能VIew
   self.outskirtsAlphView = [[UIView alloc]init];
    self.outskirtsAlphView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
    [self addSubview:self.outskirtsAlphView];
    self.UpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.UpButton setBackgroundColor:[UIColor clearColor]];
    [self.outskirtsAlphView addSubview:self.UpButton];
    self.downLable = [[UILabel alloc]init];
    self.downLable.backgroundColor = [UIColor clearColor];
    self.downLable.font = [UIFont systemFontOfSize:9.5];
    [self addSubview:self.downLable];
    self.UpButton.userInteractionEnabled = NO;
    UITapGestureRecognizer *tapGet = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTheCircleView)];
    [self addGestureRecognizer:tapGet];
    
}

/** 排版 **/
-(void)layoutSubviews{
    [super layoutSubviews];
    //开始排版
    [self.outskirtsAlphView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.height.mas_equalTo(R_Y_H(110));
        make.width.equalTo(self.outskirtsAlphView.mas_height);
        make.centerX.equalTo(self.mas_centerX);
    }];
    CGFloat MarginSize = 2.5;
    [self.UpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.outskirtsAlphView.mas_left).offset(MarginSize);
        make.right.equalTo(self.outskirtsAlphView.mas_right).offset(-MarginSize);
        make.top.equalTo(self.outskirtsAlphView.mas_top).offset(MarginSize);
        make.bottom.equalTo(self.outskirtsAlphView.mas_bottom).offset(-MarginSize);
    }];
   
    [self.downLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.outskirtsAlphView.mas_bottom).offset(13.5);
        make.bottom.equalTo(self.mas_bottom);
        make.centerX.equalTo(self.UpButton.mas_centerX);
    }];
    self.downLable.textAlignment = NSTextAlignmentCenter;
    self.downLable.text = @"贷款贷款贷款";
    [self.downLable sizeToFit];
    [self.outskirtsAlphView layoutIfNeeded];
    if (self.downLable.frame.size.width>self.outskirtsAlphView.frame.size.width) {
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.downLable.mas_left);
            make.right.equalTo(self.downLable.mas_right);
        }];
 
    }else{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.outskirtsAlphView.mas_left);
            make.right.equalTo(self.outskirtsAlphView.mas_right);
        }];

    }
    //设置圆形
    self.outskirtsAlphView.layer.cornerRadius = self.outskirtsAlphView.frame.size.width/2.0;
    [self.UpButton setBackgroundImage:[UIImage imageNamed:@"home_loan_applyok_selected"] forState:UIControlStateNormal];
    [self.UpButton setBackgroundImage:[UIImage imageNamed:@"home_loan_applyok_normal"] forState:UIControlStateSelected];
}
-(void)clickTheCircleView{
    if (self.beSelected) {
        return;
    }
    self.beSelected = YES;
    [self setViewSeletedORNomal:self.UpButton];
}

-(void)setViewSeletedORNomal:(UIButton *)button{
    if (button.selected) {
        button.selected = NO;
        self.downLable.textColor = [UIColor grayColor];
    }else{
        button.selected = YES;
        self.downLable.textColor = [UIColor whiteColor];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
